﻿CREATE PROCEDURE [dbo].[yaf_message_update](
                @MessageID INT,
                @Priority  INT,
                @Subject   NVARCHAR(100),
                @Flags     INT,
                @Message   NTEXT)
AS
    BEGIN
        DECLARE  @TopicID INT
        DECLARE  @ForumFlags INT
        SET @Flags = @Flags & ~ 16
        SELECT @TopicID = a.TopicID,
               @ForumFlags = c.Flags
        FROM   yaf_Message a,
               yaf_Topic b,
               yaf_Forum c
        WHERE  a.MessageID = @MessageID
        AND b.TopicID = a.TopicID
        AND c.ForumID = b.ForumID
        IF (@ForumFlags & 8) = 0
        SET @Flags = @Flags | 16
        UPDATE yaf_Message
        SET    Message = @Message,
               Edited = Getdate(),
               Flags = @Flags
        WHERE  MessageID = @MessageID
        IF @Priority IS NOT NULL
        BEGIN
            UPDATE yaf_Topic
            SET    Priority = @Priority
            WHERE  TopicID = @TopicID
        END
        IF NOT @Subject = ''
           AND @Subject IS NOT NULL
        BEGIN
            UPDATE yaf_Topic
            SET    Topic = @Subject
            WHERE  TopicID = @TopicID
        END
        -- If forum is moderated, make sure last post pointers are correct
        IF (@ForumFlags & 8) <> 0
        EXEC yaf_topic_updatelastpost
    END


